﻿CREATE procedure dbo.SaveCategory
(
		@Name varchar(50),
		@Description varchar(2000) = '',
		@CategoryID int = null OUTPUT
	)

AS

DECLARE @iCount int

IF isnull(@CategoryID,0) = 0
	BEGIN
		INSERT INTO dbo.Category
		                      (Name, Description)
			VALUES     (@Name,@Description)
		
		SET @CategoryID = SCOPE_IDENTITY()
		
	END 
ELSE
	BEGIN
		SET @iCount = (SELECT Count(Id) FROM dbo.Category WHERE Id = @CategoryID)
		
		if @iCount > 0 
			BEGIN
				UPDATE    dbo.Category
				SET              Name = @Name, Description = @Description WHERE ID = @CategoryID
			END
		ELSE
			BEGIN
				INSERT INTO dbo.Category
		                      (Name, Description)
				VALUES     (@Name,@Description)
		
				SET @CategoryID = SCOPE_IDENTITY()
			END
	END
	
	RETURN


